<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!-- <el-form-item label="ID" prop="id">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="手机号码">
            <el-input v-model="form.account" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="form.userName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="公司名称">
            <el-input v-model="form.companyName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="微信openId">
            <el-input v-model="form.wechatOpenId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="权限级别">
            <el-select v-model="form.level" placeholder="请选择">
              <el-option label="游客" :value="0" />
              <el-option label="一级供销商" :value="1" />
              <el-option label="二级供销商" :value="2" />
              <el-option label="三级供销商" :value="3" />
              <el-option label="零售商" :value="4" />
            </el-select>
            <!-- <el-input v-model="form.level" style="width: 370px;" /> -->
          </el-form-item>
          <!-- <el-form-item label="是否删除 0：否 1：是">
            <el-input v-model="form.isDel" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="创建时间">
            <el-input v-model="form.createTime" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="更新时间">
            <el-input v-model="form.updateTime" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="更新用户">
            <el-input v-model="form.updateUser" style="width: 370px;" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="ID" />
        <el-table-column v-if="columns.visible('account')" prop="account" label="手机号码" />
        <el-table-column v-if="columns.visible('nickName')" prop="nickName" label="昵称" />
        <el-table-column v-if="columns.visible('userName')" prop="userName" label="姓名" />
        <el-table-column v-if="columns.visible('companyName')" prop="companyName" label="公司名称" />
        <el-table-column v-if="columns.visible('wechatOpenId')" prop="wechatOpenId" label="微信openId" />
        <el-table-column v-if="columns.visible('level')" prop="level" label="权限级别">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.level === 0 ">游客</el-tag>
            <el-tag v-if="scope.row.level === 1 ">一级</el-tag>
            <el-tag v-if="scope.row.level === 2 ">二级</el-tag>
            <el-tag v-if="scope.row.level === 3 ">三级</el-tag>
            <el-tag v-if="scope.row.level === 4 ">零售</el-tag>
          </template>
        </el-table-column>

        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','supplierUser:edit','supplierUser:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSupplierUser from '@/api/supplier/supplierUser'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { formatTimeThrid, formatTimeTwo, parseTime } from '@/utils/index'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '代理商用户', url: 'api/supplierUser', sort: 'id,desc', crudMethod: { ...crudSupplierUser }})
const defaultForm = { id: null, account: null, nickName: null, userName: null, companyName: null, wechatOpenId: null, level: null, isDel: null, createTime: null, updateTime: null, updateUser: null }
export default {
  name: 'SupplierUser',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'supplierUser:add'],
        edit: ['admin', 'supplierUser:edit'],
        del: ['admin', 'supplierUser:del']
      },
      rules: {
        id: [
          { required: true, message: 'ID不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'nickName', display_name: '昵称' },
        { key: 'userName', display_name: '姓名' },
        { key: 'companyName', display_name: '公司名称' }
      ]
    }
  },
  methods: {
    formatTimeThrid,
    formatTimeTwo,
    parseTime,
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    }
  }
}
</script>

<style scoped>

</style>
